﻿using codeparser.net.v2.Contracts;
using codeparser.net.v2.SyntaxHighlighter;

using System;
using System.Collections.Generic;
using System.Xml;

namespace codeparser.net.v2.Entities
{
    /// <summary>
    /// Represents a tag rule.
    /// </summary>
    public class TagRule : IXmlWriter
    {
        /// <summary>
        /// Gets or sets the rule for the opening tag.
        /// </summary>
        /// <value>The rule for the opening tag.</value>
        public string OpeningTagRule
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the rule for the closing tag.
        /// </summary>
        /// <value>The rule for the closing tag.</value>
        public string ClosingTagRule
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the syntax highlighter.
        /// </summary>
        /// <value>The syntax highlighter.</value>
        public SyntaxHighlighterStrategy SyntaxHighlighter
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the language for the syntax highlighter.
        /// </summary>
        public Language SyntaxHighlighterLanguage
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the TagRule type.
        /// </summary>
        /// <param name="rule">The rule.</param>
        public TagRule(string rule)
            : this(rule, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the TagRule type.
        /// </summary>
        /// <param name="openingTagRule">The rule for the opening tag.</param>
        /// <param name="closingTagRule">The rule for the closing tag.</param>
        public TagRule(string openingTagRule, string closingTagRule)
            : this(openingTagRule, closingTagRule, SyntaxHighlighterStrategy.None, Language.None)
        {
        }

        /// <summary>
        /// Initializes a new instance of the TagRule type.
        /// </summary>
        /// <param name="openingTagRule">The rule for the opening tag.</param>
        /// <param name="closingTagRule">The rule for the closing tag.</param>
        /// <param name="syntaxHighlighter">The syntax highlighter for highlighting the content.</param>
        /// <param name="syntaxHighlighterLanguage">The language for the syntax highlighter.</param>
        public TagRule(string openingTagRule, string closingTagRule, SyntaxHighlighterStrategy syntaxHighlighter, Language syntaxHighlighterLanguage)
        {
            // Initialize the fields.
            this.OpeningTagRule = openingTagRule;
            this.ClosingTagRule = closingTagRule;
            this.SyntaxHighlighter = syntaxHighlighter;
            this.SyntaxHighlighterLanguage = syntaxHighlighterLanguage;
        }

        /// <summary>
        /// Saves the tag rule to the specified XML writer.
        /// </summary>
        /// <param name="writer">The XML writer.</param>
        void IXmlWriter.Save(XmlWriter writer)
        {
            // Save the rule.
            writer.WriteStartElement("Rule");
            {
                writer.WriteAttributeString("OpeningTagRule", this.OpeningTagRule);
                if (this.ClosingTagRule != null)
                {
                    writer.WriteAttributeString("ClosingTagRule", this.ClosingTagRule);
                }
                writer.WriteStartElement("SyntaxHighlighter");
                {
                    writer.WriteAttributeString("Type", this.SyntaxHighlighter.ToString());
                    if (this.SyntaxHighlighter != SyntaxHighlighterStrategy.None)
                    {
                        writer.WriteAttributeString("Language", this.SyntaxHighlighterLanguage.ToString());
                    }
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
    }
}